import { CollectionUtils } from "@chulei-ui/utils";
import type { UniverseSystem } from "./UniverseSystem";
import type ExcelJs from "@scvzerng/exceljs";

export class UniverseStruct {
  structure_id!: number;
  name!: string;
  system?: UniverseSystem;
  system_id!: number;
  type!: string;

  static fromSheet(sheet: ExcelJs.Worksheet, systems: UniverseSystem[]) {
    const structs: UniverseStruct[] = [];
    const systemMap = CollectionUtils.array2Map<UniverseSystem>(systems, (system: UniverseSystem) => system.system_id);
    sheet.eachRow((row, index) => {
      if (index > 1) {
        const id = row.getCell(1).text!;
        const name = row.getCell(2).text!;
        const type = row.getCell(3).text!;
        const systemId = Number(row.getCell(4).text!);
        const struct = new UniverseStruct();
        const system = systemMap[systemId];
        struct.structure_id = Number(id);
        struct.name = name;
        struct.type = type;
        struct.system = system;
        system.structs.push(struct);
        structs.push(struct);
      }
    });
    return structs;
  }
}
